#include "print.h"
#include <termios.h>
#include<unistd.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include "Dload.h"
#include "modemcontrol.h"

fd_set rfds;
int uart_fd = -1;
struct timeval timeout;

//static char *modem_state_path = "/sys/class/modem/ttyM0/modem_onoff";
static char *modem_state_path = "/dev/modem";
int set_m1190_power(int on) 
{
	int fd = 0;

	fd = open("/dev/modem", O_RDWR);
	if(fd > 0) 
	{
		ioctl(fd, on ? MODEM_RESET : MODEM_RESET);
		close (fd);                                   
	}

	return 0;
}
int set_m7281_power(int on) 
{
	int fd = 0;

	fd = open("/dev/modem", O_RDWR);

	if(fd > 0) 
	{
		ioctl(fd, on ? MODEM_RESET : MODEM_PWR_OFF);
		close(fd); 
	}	
	
	return 0;
}

int open_com(char *dev)
{	
	uart_fd = open(dev, O_RDWR | O_NOCTTY );//| O_NDELAY );//| O_NONBLOCK);//| O_NDELAY);
	LOGI("open_com fd=%d", uart_fd);
	if (uart_fd != -1)
	{
		struct termios options;
		// The old way. Let's not change baud settings
		//fcntl(uart_fd, F_SETFL, 0);
			
		// get the parameters
		tcgetattr(uart_fd, &options);
			
			
		cfsetispeed(&options, B115200);
		cfsetospeed(&options, B115200);
			
		// Enable the receiver and set local mode...
		options.c_cflag |= (CLOCAL | CREAD);
			
		// No parity (8N1):
		options.c_cflag &= ~PARENB;
		options.c_cflag &= ~CSTOPB;
		options.c_cflag &= ~CSIZE;
		options.c_cflag |= CS8;

        options.c_iflag &= ~IXOFF;
		options.c_iflag &= ~IXON;
		options.c_iflag &= ~IXANY;
			
		// enable hardware flow control (CNEW_RTCCTS)
		options.c_cflag &= ~CRTSCTS;
			
		// set raw input
		options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
		options.c_iflag &= ~(INLCR | ICRNL | IGNCR );
			
		// set raw output
		options.c_oflag &= ~OPOST;
		options.c_oflag &= ~OLCUC;
		options.c_oflag &= ~ONLRET;
		options.c_oflag &= ~ONOCR;
		options.c_oflag &= ~OCRNL;

		options.c_cc[VMIN]=8;
		tcflush(uart_fd, TCIFLUSH);
		// Set the new options for the port...
		tcsetattr(uart_fd, TCSANOW, &options);
	}
	else
	    return 0;
	return 1;
}

int down_percent(char* szStep,int status, int nCurPos, int nLen)
{
	LOGE("[=====%s======]: %s:%d/%d %.2f%%.\r\n", __func__, szStep, nCurPos, nLen, (float)(nCurPos*100)/(nLen>0?nLen:1));

	return 1;
}

int scom_change_baud(int nBaud)
{
    struct termios options;
	tcgetattr(uart_fd, &options);		
			
	//cfsetispeed(&options, B460800);
	//cfsetospeed(&options, B460800);
	cfsetispeed(&options, B921600);
	cfsetospeed(&options, B921600);

	int ret = tcsetattr(uart_fd, TCSANOW, &options);

	tcgetattr(uart_fd, &options);
	LOGE("cfgetospeed:%d", cfgetospeed(&options));
	LOGE("cfgetispeed:%d", cfgetispeed(&options));

	return ret ? 0 : 1;
}

void printBuf(char* buffer, int len)
{
	char debuff[73] = {0};
	int i = 0;
	for(i ;i < len ; i++)
	{
		sprintf(debuff, "%s %2X", debuff, buffer[i]);
		if((i + 1) % 24 == 0)
		{
			LOGE("%s", debuff);
			memset(debuff, 0, 73);
		}
	}
	LOGE("%s", debuff);

}

int write_com_m7281(char *buffer, int len)
{
	int num;
	int i=0;
	int total = 0;
	while(total < len) {
            tcflush(uart_fd, TCIFLUSH);
	    num = write(uart_fd, buffer, len);
	    if(num < 0)
	    {
		break;
            }
	    total += num;
	}
#ifdef DEBUG
	LOGE("write_com buffer num =%d----->\n", num);
	printBuf(buffer, len);
	LOGE("write_com buffer end ----->\n");
#endif
	return total;	
}

int write_com_m1190(char *buffer, int len)
{
	int num;
	int i=0;
	tcflush(uart_fd, TCIFLUSH);
	num = write(uart_fd, buffer, len);
#ifdef DEBUG
	LOGE("write_com buffer num =%d----->\n", num);
	printBuf(buffer, len);
	LOGE("write_com buffer end ----->\n");
#endif
	return num;	
}

int read_com(char *buffer, int len , int time)
{
	int num = 0;
	int i=0;
	FD_ZERO(&rfds);
	FD_SET(uart_fd, &rfds);
	if (m_iLastSentPktType == 0x7E)
	{
	    timeout.tv_sec = 0;		
	    timeout.tv_usec = 10000*time;
    }else
    {
    	timeout.tv_sec = 1;		
	    timeout.tv_usec = 0;
	}
	if (select(uart_fd + 1, &rfds, NULL, NULL, &timeout) > 0)
	{
	    if (FD_ISSET(uart_fd, &rfds))
	    {
	        num = read(uart_fd, buffer, len);
#ifdef DEBUG
	        LOGE("read_com buffer num =%d <------\n", num);
	        printBuf(buffer, num);
			LOGE("read_com buffer end <------\n");
#endif
        }
	}else
	    LOGE("ReadPacket Timeout");
	return num;
}

int close_com()
{
	close(uart_fd);
	
	return 1;
}
